package org.example.petadoption.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.sf.jsqlparser.util.SelectUtils;
import org.example.petadoption.entity.Product;
import org.example.petadoption.entity.ProductInfoDTO;
import org.example.petadoption.entity.ResultBody;
import org.example.petadoption.entity.User;
import org.example.petadoption.service.IProductCategoryService;
import org.example.petadoption.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.init.ScriptUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author PetAdoption
 * @since 2025-10-20
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    IProductService service;


    //商品搜索
    @PostMapping("/selectProduct")
    public ResultBody selectProduct(String keyword,int categoryId,int pageIndex,int pageSize) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)) {
            wrapper.like(Product::getName, keyword).eq(Product::getStatus,1);
        }
        if (categoryId > 0) {
            wrapper.eq(Product::getCategoryId, categoryId).eq(Product::getStatus,1);
        }
        if (pageIndex < 0 || StringUtils.isEmpty(keyword)) {
            wrapper.eq(Product::getStatus,1);
        }
        if (categoryId > 0 || !StringUtils.isEmpty(keyword)){
            wrapper.like(Product::getName, keyword).eq(Product::getCategoryId, categoryId).eq(Product::getStatus,1);
        }

        Page<Product> page = new Page<>(pageIndex, pageSize);
        List<Product> data = service.list(page,wrapper);
        ResultBody<List<Product>> body = ResultBody.success("查询成功",data);
        body.setTotalPage(page.getTotal());
        return body;
    }

    //所有商品信息
    @GetMapping("/allProduct")
    public ResultBody allProduct() {
        List<Product> data = service.list();
        return ResultBody.success("所有商品信息",data);
    }

    //商品详情-单条商品信息-商品id
    @PostMapping("/oneProduct")
    public ResultBody oneProduct(String id) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getId, id);
        Product data = service.getOne(wrapper);
        return ResultBody.success("id " + id + "的商品信息", data);
    }

    //商品详情-多条商品信息-商家id
    @PostMapping("/spProduct")
    public ResultBody spProduct(String id) {
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getMerchantId, id);
        List<Product> data = service.list(wrapper);
        return ResultBody.success("id " + id + "的商品信息", data);
    }

    //添加商品
    @PostMapping("/addProduct")
    public ResultBody addProduct(Product product) {

        product.setStatus((byte) 0);

        if (product.getMerchantId() <= 0) {
            return ResultBody.fail("商家ID不正确");
        }
        if (product.getCategoryId() <= 0) {
            return ResultBody.fail("商品分类ID不正确");
        }
        if (StringUtils.isEmpty(product.getName())) {
            return ResultBody.fail("商品名称不正确");
        }
        if (product.getPrice() != null) {
            return ResultBody.fail("价格不正确");
        }
        if (product.getStock() <= 0) {
            return ResultBody.fail("库存不正确");
        }
        if (StringUtils.isEmpty(product.getDescription())) {
            return ResultBody.fail("商品介绍不能为空");
        }
        if (StringUtils.isEmpty(product.getCoverImage())) {
            return ResultBody.fail("商品图片链接不能为空");
        }

        boolean result = service.save(product);
        if (!result) {
            return ResultBody.success("添加失败");
        }
        return ResultBody.success("添加成功");
    }

    //修改商品
    @PostMapping("/upProduct")
    public ResultBody updateProduct(Product product) {
        if (product.getId() <= 0) {
            ResultBody.fail("");
        }
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Product::getId, product.getId());
        Product data = service.getOne(wrapper);
        if (data == null) {
            ResultBody.fail("");
        }

        Product old = service.getById(product.getId());
        if (old.getStock() == 0) {
            service.removeById(product.getId());
            return ResultBody.fail("库存为0自动删除商品");
        }

        boolean result = service.updateById(product);
        return result?ResultBody.success("修改成功",result):ResultBody.fail("修改失败");
    }

    //管理员获取商品
    @GetMapping("/infoProduct")
    public ResultBody getProductInfo() {
        List<ProductInfoDTO> result = service.getAllProductInfo();
        return ResultBody.success("审核内容",result);
    }


}
